// JavaScript Document

$(function(){
	
	/* 表格部分 */
	(function(){
		//读取数据
		var datas = new Array();
		$("#result ul li ul li").each(
			function() {
				// 属性，变量名，不要以大写开头，以后学了面向对象与类名区别开来
				// 整合一个对象
				var data = {
					year : $(this).parent().parent().parent().parent()
							.find("h2").html(),// 获取年份
					country : $(this).attr("data-country") || "",// 获取城市
					title : $(this).attr("title") || "",// 获取奖牌
					name : $(this).html() || "",// 选手名字
					skillID : $(this).parent().parent().find("div").html()
				// 技能编号
				};
				datas.push(data); // 写入数组
			}
		);
		//绘制表格
		var string = '';
		for ( var i in datas) {
			var data = datas[i];
			string += "<tr><td>" + data.year + "</td>" + "<td class='click'>"
					+ data.skillID + "</td>" + "<td class='click'>"
					+ data.country + "</td>" + "<td class='click'>"
					+ data.title + "</td>" + "<td class='click'>" + data.name
					+ "</td></tr>"
		}
		$("#Table tbody").html(string);

		//筛选数据
		function search(){
			var keys = $("#search").val() , lowerCase = !$("#cbox").get(0).checked;	//搜索框和大小写复选框
			var nums = 0, list = new Array();
			if(lowerCase) keys = keys.toLowerCase();
			keys = keys.split("+");	//以+号分割成数组
			$("#Table tbody tr").hide().each(function(i){
				var data = datas[i], founded = true;	//获取每个对象
				for(var cond in keys){		//循环条件数组
					var key = keys[cond];	//当前条件
					var keyok = false;		//默认不满足条件
					for(var now in data){
						if(now == 'Year') continue;	//不判断年份
						if((lowerCase ? data[now].toLowerCase() : data[now]).indexOf(key) >= 0){	//是否大小写要求,满足条件判断
							if(key != '' && list.length < 5 && list.indexOf(data[now]) < 0){
								list.push(data[now]);		//数组中已存在就不写入,最多5个
							}
							keyok = true; 	//找到符合数据
							break;	//结束循环
						}
					}
					founded = founded && keyok;		//存在满足条件数据
				}
				if(founded){
					$(this).show();		//显示出来
					nums++;
				}
			});
			//没有查询到数据的提示信息
			if(nums == 0){
				$(".error").show();
				$(".error span").html(keys);
			}else{
				$(".error").hide();
			}
			var listbox = '';
			for(var t=0;t<list.length;t++){
				listbox += "<option value='" + list[t] +"'/>";		//定义提示信息
			}
			$("datalist#listbox").html(listbox);		//存储提示信息
		}
		
		//表格元素点击搜索
		$("#Table tbody tr td.click").click(function (e){
			var val = this.innerHTML.replace(/^[a-z0-9A-Z]+\s+\-\s+/g, ""), textbox = $("#search").val();
			if(textbox != "" && textbox.indexOf(val) >= 0) return false;	//搜索条件重复,在搜索框中重复
			if(e.shiftKey && textbox != "") val = textbox + "+" + val;		//按Shift键追加条件
			$("#search").val(val);		//筛选过的value值填入搜索框
		});
		
		
		//搜索框响应事件
		var ipt = $("input#search"), prev = '';
		//Ctrl+S
		ipt.keypress(function(e){
			if((e.charCode == 115 || e.charCode == 83) && e.ctrlKey){
				localStorage['i_s'] = ipt.val();
				localStorage['c_s'] = $("#cbox").get(0).checked;
				e.preventDefault();	 	//阻止按键的提交,防止网页保存
			}
		});
		//鼠标点击datalist
		setInterval(function(){
			if(prev != ipt.val()){
				search();
			}
			prev = ipt.val();
		}, 100);
		
		//复选框响应事件
		$("#cbox").click(function(){
			setTimeout(search,1);
		});
		
		//载入搜索信息
		if(typeof(localStorage['i_s']) != 'undefined'){
			$("#search").val(localStorage['i_s']);
			$("#cbox").attr("checked", eval(localStorage['c_s']));	//eval计算返回结果值
		}	
	})();

	/*显示隐藏文字*/
	$("span.c a.btncon").click(function(){
		var hidtext = $(this).parent().parent().find('div div.hidden-text');
		hidtext.show();
		var h = hidtext.get(0).offsetHeight;
		hidtext.css('height','0px');
		$(this).hide();
		setTimeout(function(){
			hidtext.css('height',h+'px');
		},100);
		setTimeout(function(){
			bgmove();
		},500);
	});
	//背景变化部分
	function bgmove(){
		var section = [{'id':'#about','left':870,'top':60},
					   {'id':'#competition41','left':20,'top':60},
					   {'id':'#competition40','left':860,'top':30}];
		var bodytop = $.scrollTop();
		
		//小图标部分
		var nos = '';	//防止再执行
		for(var i in section){
			var id = section[i].id, sec = $(id);
			var now = sec.get(0).getBoundingClientRect();	//返回一个矩形对象距离窗口的距离	top顶部
			if(bodytop < 50 && nos != 'top'){
				nos = 'top';
				logo(55,0);		//恢复到原位
			}else if(now.top >= 0 && nos == ''){
				nos = id;
				logo(section[i].left, sec.get(0).offsetTop + section[i].top);	
				$('newnav li').attr('style','');
				$('newnav li a[href="'+id+'"]').parent().css('background','#333');
			}
			
			//背景部分
			var offsetTop = sec.get(0).offsetTop;
			var one = offsetTop / 100;
			var num = bodytop / one;
			var bgxy = '';
			if(i == 0){
				bgxy = '5% '+(-50+num)+'%, 5% '+(185 - num)+'%';
			}else if(i == 1){
				bgxy = '90% '+(-50+num)+'%, 95% '+(130 - num)+'%';
			}else if(i == 2){
				bgxy = '10% '+(-50+num)+'%, 0% '+(130 - num)+'%';
			}
			sec.get(0).style.backgroundPosition = bgxy;
		}
	}
	bgmove();
	
	var hasFocus = false;
	
	/*小图标滑动*/
	function logo(x,y){
		if(!hasFocus){
			var litICO = $("div#handSymbol");
			litICO.css('left',x+'px').css('top',y+'px');
		}
	}
	
	/*文本框焦点*/
	var ipt = $("input#search").get(0);
	ipt.onfocus = function(){
		logo(-20,$("#result").get(0).offsetTop);
		hasFocus = true;
	};
	ipt.onblur = function(){
		hasFocus = false;
		bgmove();
	};
	
	/*LOGO点击返回页头*/
	function toUp(step){
		window.scrollBy(0, step);
		if($.scrollTop() > 0) { 
			setTimeout(function (){toUp(step);},10);
		}
	}
	$("div#handSymbol").click(function(){
		toUp($.scrollTop() / -15);
	});

	/*菜单栏部分*/
	$('nav a,newnav a').click(function(){
		scrollmove.on($(this).attr('href'));
		return false;//阻止地址栏更新地址
    });
		
	//滚动
	var scrollmove = {
		on:function(id){
			var start = new Date();	
			var current = $.scrollTop();
			var witch = ($(id).get(0).offsetTop - 40) - current;
			var ms = witch < 0 ? witch * -1 : witch;
			scrollmove.off();
			this.timer = setInterval(function(){	
				var p = (new Date() - start) / ms * 1;
				if(p > 1) p = 1;
				scrollTo(0,current + witch * p);
				if(p == 1){
					scrollmove.off();
				}
			},1);
		},
		off:function(){
			clearInterval(this.timer);
		}
	}
	
	window.addEventListener("scroll", function(){
		$('newnav').get(0).style.display = $.scrollTop() > 70 ? 'block' : 'none';	//切换菜单
		bgmove();
	});
	
	//banner动画部分
	$(".movebtn").click(function(){
		slider.on();
		var dir = this.className.replace('movebtn ','');
		animate(dir);
		return false;
	});
	var slider = {
		on:function(){
			slider.off();
			this.timer = setInterval(function(){
				animate('next');
			},5000);
		},
		off:function(){ clearInterval(this.timer); }  
	}
	slider.on();	//调用动画
	function animate(dir){
		var lft = tmp = $('#slider .lft').get(0);	//左边
		var cen = $('#slider .cen').get(0);	//中间
		var rgt = $('#slider .rgt').get(0);	//右边
		if(dir == 'next'){
			lft = cen, cen = rgt, rgt = tmp;
		}else{
			lft = rgt, rgt = cen, cen = tmp;
		}
		lft.className = 'lft', lft.style.zIndex = 20;
		cen.className = 'cen', cen.style.zIndex = 30;
		rgt.className = 'rgt', rgt.style.zIndex = 20;
	}
});